package cn.dapeng.tree.binary;

/**
 * 判断一个数是否是镜面树
 * <p>
 * 对于每一颗子树来说， 左节点和右节点的值都相同
 */
public class CheckMirrorTree {

    public static boolean isMirrorTree(TreeNode head) {
        return isMirrorTree(head, head);
    }

    private static boolean isMirrorTree(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null ^ right == null) {
            return false;
        }

        return left.val.equals(right.val) && isMirrorTree(left.left, right.right) && isMirrorTree(left.right, right.left);
    }

    public static void main(String[] args) {
        TreeNode head = new TreeNode(0);
        head.left = new TreeNode(1);
        head.right = new TreeNode(1);

        head.left.left = new TreeNode(2);
        head.right.right = new TreeNode(2);

        head.left.right = new TreeNode(3);
        head.right.left = new TreeNode(3);

        System.out.println(isMirrorTree(head));
    }


}
